﻿#region Copyright

// Majesty of Omega - The next evolution of macro management space strategy
// 
// Copyright 2007-2009 André Claaßen
// 
// This file is protected by the terms and conditions of the
// Microsoft Community License (Ms-CL 1.1), a copy of which should
// have been distributed along with this software. If not,
// you may find the license information at the following URL:
// 
// http://www.microsoft.com/resources/sharedsource/licensingbasics/communitylicense.mspx

#endregion

#region

using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Windows;
using Majesty_of_Omega.GUI.Pages.Game.Research.Parts;
using Majesty_of_Omega.Logic.Generator;
using Majesty_of_Omega.Model.Research;

#endregion

namespace Majesty_of_Omega.GUI.Controller
{
    /// <summary>
    /// Controller class for the <see cref="TechnologyMatrixPart"/> UserControl. 
    /// </summary>
    /// <remarks>
    /// First: This Controller provides a ViewModel for databinding purposes of the wpf user control. <para/>
    /// Second: The complete interaction logic of the page will be handeled through this controller.
    /// </remarks>
    public class TechMatrixController : InGameController, INotifyPropertyChanged
    {
        #region Dependency Property  FirstVisibleLevel 

        /// <summary>
        /// Dependency property FirstVisibleLevel
        /// </summary>
        public static readonly DependencyProperty FirstVisibleLevelProperty =
            DependencyProperty.Register("FirstVisibleLevel", typeof (int), typeof (TechMatrixController), new UIPropertyMetadata(1, OnFirstVisibleLevelValueChanged));

        /// <summary>
        /// Gets or sets the FirstVisibleLevel
        /// </summary>
        /// <value>The value.</value>
        public int FirstVisibleLevel
        {
            get { return (int) GetValue(FirstVisibleLevelProperty); }
            set { SetValue(FirstVisibleLevelProperty, value); }
        }

        #endregion

        /// <summary>
        /// Change something, when the value of the dependency property FirstVisibleLevel
        /// </summary>
        /// <param name="d">instance of TechMatrixController</param>
        /// <param name="e">the new and old values</param>
        private static void OnFirstVisibleLevelValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            TechMatrixController controller = (TechMatrixController) d;
            controller.PropertyChanged(controller, new PropertyChangedEventArgs("LevelNumbers") );
        }
        
        /// <summary>
        /// Initializes a new instance of the <see cref="ResearchPageController"/> class.
        /// </summary>
        /// <remarks>
        /// This constructor serves only for testing purposes.
        /// </remarks>
        public TechMatrixController()
        {
            this.Game = TestDataGenerator.TestSaveGame;
        }

        /// <summary>
        /// Called when [save game changed].
        /// </summary>
        protected override void OnSaveGameChanged()
        {
            SelectedTechSchoolResearch = saveGame.HumanPlayer.ResearchManager.TechSchoolResearchs[1];
            InvokePropertyChanged("TechSchoolResearchList");
            InvokePropertyChanged("MaxTechLevel");
            InvokePropertyChanged("SelectedTechSchoolResearch");
            InvokePropertyChanged("ResearchedTechFields");
        }

        /// <summary>
        /// Gets the research manager.
        /// </summary>
        /// <value>The research manager.</value>
        public ResearchManager ResearchManager
        {
            get { return saveGame.HumanPlayer.ResearchManager; }
        }

        /// <summary>
        /// 
        /// </summary>
        private TechSchoolResearch selectedTechSchoolResearch;

        /// <summary>
        /// Gets or sets the selected tech school research.
        /// </summary>
        /// <value>The selected tech school research.</value>
        public TechSchoolResearch SelectedTechSchoolResearch
        {
            get
            {
                return selectedTechSchoolResearch;
            }
            set
            {
                if (selectedTechSchoolResearch == value) return;
                selectedTechSchoolResearch = value;
                InvokePropertyChanged("SelectedTechSchoolResearch");
                InvokePropertyChanged("ResearchedTechFields");
            }
        }

        /// <summary>
        /// Gets the tech school research list.
        /// </summary>
        /// <value>The tech school research list.</value>
        public IEnumerable<SelectableTechSchoolResearchItem> TechSchoolResearchList
        {
            get
            {
                return from tfr in ResearchManager.TechSchoolResearchs
                       select new SelectableTechSchoolResearchItem(this, tfr);
            }
        }

        /// <summary>
        /// Gets the researched tech fields for the selected technology school
        /// </summary>
        /// <value>The researched tech fields.</value>
        public IEnumerable<TechFieldResearch> ResearchedTechFields
        {
            get
            {
                return this.SelectedTechSchoolResearch.ResearchedTechFields;
            }
        }

        /// <summary>
        /// Occurs when a property value changes.
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        /// <summary>
        /// Notify wpf, that a databound property of the <see cref="ResearchPageController" /> has changed
        /// </summary>
        /// <param name="propertyName">Name of the property.</param>
        internal void InvokePropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        /// <summary>
        /// Gets the levels.
        /// </summary>
        /// <remarks>
        /// This is a new remark for testing.
        /// </remarks>
        /// <value>The levels.</value>
        public IEnumerable<int> LevelNumbers
        {
            get
            {
                List<int> levels = new List<int>();
                for (int i = FirstVisibleLevel; i<(FirstVisibleLevel+UIConstants.MaximumVisibleTechLevels); i++)
                {
                    levels.Add(i);
                }
                return levels;
            }
        }

        #region nested classes for databinding purposes

        /// <summary>
        /// a wrapper class for databinding with the stupid radio buttons for the tech schools
        /// </summary>
        /// <remarks>
        /// The trick is, that you now can bind the selected state to a property
        /// </remarks>
        public class SelectableTechSchoolResearchItem
        {
            private readonly TechMatrixController controller;
            private readonly TechSchoolResearch techSchoolResearch;

            /// <summary>
            /// Initializes a new instance of the <see cref="SelectableTechSchoolResearchItem"/> class.
            /// </summary>
            /// <param name="controller">The controller.</param>
            /// <param name="tfr">The TFR.</param>
            public SelectableTechSchoolResearchItem(TechMatrixController controller, TechSchoolResearch tfr)
            {
                this.controller = controller;
                techSchoolResearch = tfr;
            }

            /// <summary>
            /// Gets the tech school research.
            /// </summary>
            /// <value>The tech school research.</value>
            public TechSchoolResearch TechSchoolResearch
            {
                get { return techSchoolResearch; }
            }

            /// <summary>
            /// Gets or sets a value indicating whether this instance is selected.
            /// </summary>
            /// <value>
            /// 	<c>true</c> if this instance is selected; otherwise, <c>false</c>.
            /// </value>
            /// <remarks>
            /// This property is databound with the radio buttons for the Tech Schools in the Tech Matrix
            /// </remarks>
            public bool IsSelected
            {
                get { return controller.SelectedTechSchoolResearch == techSchoolResearch; }
                set
                {
                    if (value && !IsSelected)
                        controller.SelectedTechSchoolResearch = techSchoolResearch;
                }
            }
        }

        #endregion
    }
}